package com.cn.yygh.hosp.controller.api;

import com.cn.yygh.common.exception.YyghException;
import com.cn.yygh.common.helper.HttpRequestHelper;
import com.cn.yygh.common.result.Result;
import com.cn.yygh.common.result.ResultCodeEnum;
import com.cn.yygh.common.utils.MD5;
import com.cn.yygh.hosp.service.HospitalService;
import com.cn.yygh.hosp.service.HospitalSetService;
import com.cn.yygh.hosp.service.ScheduleService;
import com.cn.yygh.model.hosp.Hospital;
import com.cn.yygh.model.hosp.Schedule;
import com.cn.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private ScheduleService scheduleService;

    //删除排班接口
    @ApiOperation(value = "删除排班接口")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");//医院编号
        //必填
        String hosScheduleId = (String)paramMap.get("hosScheduleId");//排班编号
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //todo 签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();

    }
    //查询排班接口
    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传递过来的信息
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");//医院编号
        //非必填
        String depcode = (String) paramMap.get("depcode");//科室编号
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //todo 签名校验
//        if (!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);

    }

    //上传排班接口
    @ApiOperation(value = "上传排班接口")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //利用工具类 HttpRequestHelper 转换为Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //todo 签名校验
        scheduleService.save(paramMap);
        return Result.ok();

    }

    /*
    查询医院
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //利用工具类 HttpRequestHelper 转换为Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取传来的医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //1. 获取传递过来的签名，进行MD5加密  ;此时获得的签名是已加密的，HttpRequestHelper工具类中有Md5加密
        String hospSign = (String) paramMap.get("sign");
        //2. 根据传递过来的 医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3. 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4. 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现根据 医院编号 查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /*
    controller如何得到post的数据
        request 得到传递过来的数据，传到方法中
     */
    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //利用工具类 HttpRequestHelper 转换为Object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        /*
        签名校验
        674c4139707728439a6510eae12deea9
         */
        //1. 获取传递过来的签名，进行MD5加密  ;此时获得的签名是已加密的，HttpRequestHelper工具类中有Md5加密
        String hospSign = (String) paramMap.get("sign");
        //2. 根据传递过来的 医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3. 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4. 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中 “+” 转换为了 " ",要再转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);
        //调用service方法
        hospitalService.save(paramMap);
        return Result.ok();
    }
}
